package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoStockService;

public class StoStockServiceImpl extends ServiceImpl<TbStoStock, Integer>
		implements StoStockService {

	@Override
	public List<StoStockDTO> findByListDate(Date listingDate, int offset,int count) {
		String hql="from TbStoStock as tss where tss.listingDate=? limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{listingDate,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByListDateRange(Date startDate, Date endDate,int offset, int count) {
		String hql="from TbStoStock as tss where tss.listingDate between ? and ? limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{startDate,endDate,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByStockBoard(String stockBoard, int offset,int count) {
		String hql="from TbStoStock as tss where tss.stockBoard=? limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{stockBoard,count,offset}));
	}

	@Override
	public StoStockDTO findByStockCode(String stockCode) {
		String hql="from TbStoStock as tss where tss.stockCode=?";
		if(this.findByProperty(hql, stockCode).size()>0){
			return getDtoFromPojo(this.findByProperty(hql, stockCode).get(0)); 
		}else{
			return null;
		}
		
	}
	public void update(TbStoStock tbStoStock){
		this.dao.update(tbStoStock);
	}
	
	@Override
	public List<StoStockDTO> findByStockName(String stockName, int offset,int count) {
		String hql="from TbStoStock as tss where tss.stockName like %?% limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{stockName,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByStockType(String stockType, int offset,int count) {
		String hql="from TbStoStock as tss where tss.stockType=? limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{stockType,count,offset}));
	}

	
	@Override
	public List<StoStockDTO> findByMainOperation(String mainOperation,int offset, int count) {
		String hql="from TbStoStock as tss where tss.mainOperation like %?% limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{mainOperation,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByManageArea(String manageArea, int offset,	int count) {
		String hql="from TbStoStock as tss where tss.manageArea like %?% limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{manageArea,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByCompanyNameEn(String companyNameEn,int offset, int count) {
		String hql="from TbStoStock as tss where tss.companyNameEn like %?% limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{companyNameEn,count,offset}));
	}

	@Override
	public List<StoStockDTO> findByCompanyNameZh(String companyNameZh,int offset, int count) {
		String hql="from TbStoStock as tss where tss.scompanyNameZh like %?% limit ? offset ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{companyNameZh,count,offset}));
	}

	/**
	 * 批量地将数据库持久化对象POJO的tbStoStock转换成领域模型的数据传输对象DTO的StoStockDTO
	 * @param tbStoStockList
	 * 			数据库持久化对象POJO的List数组
	 * @return List<StoStockDTO>
	 * 		领域模型的数据传输对象DTO的StoStockDTO的List数组
	 */
	public List<StoStockDTO> foreachGetDtoFromPojo(List<TbStoStock> tbStoStockList){
		List<StoStockDTO> stoStockDTOList=new ArrayList<StoStockDTO>();
		for (TbStoStock tbStoStock : tbStoStockList) {
			stoStockDTOList.add(getDtoFromPojo(tbStoStock));
		}
		return stoStockDTOList;
	}
	
	/**
	 * 将数据库持久化对象POJO的tbStoStock转换成领域模型的数据传输对象DTO的StoStockDTO
	 * @param tbStoStock
	 * 			数据库持久化对象POJO
	 * @return StoStockDTO
	 * 		领域模型的数据传输对象DTO的StoStockDTO
	 */
	public StoStockDTO getDtoFromPojo(TbStoStock tbStoStock){
		return new StoStockDTO(
				tbStoStock.getId(),tbStoStock.getStockCode(),
				tbStoStock.getStockName(),tbStoStock.getStockBoard(),
				tbStoStock.getStockType(),tbStoStock.getListingDate(),
				tbStoStock.getManageArea(),tbStoStock.getMainOperation(),
				tbStoStock.getCompanyNameZh(),tbStoStock.getCompanyNameEn(),
				tbStoStock.getDescription()
		);
	}
}
